<?php
define('FPDF_FONTPATH','font/');
require_once('fpdf.php');
require_once('gisp_admincon.php');
//require_once('qrcode.class.php');
require_once("Connections/coneccionRatania.php");
require_once("Connections/funciones_pg.php");
require_once("qrlib.php");
//include_once('rotation.php');
class PDF extends FPDF{

   
    
var $angle=0;
var $javascript;
var $n_js;

	function IncludeJS($script) {
		$this->javascript=$script;
	}

	function _putjavascript() {
		$this->_newobj();
		$this->n_js=$this->n;
		$this->_out('<<');
		$this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
		$this->_out('>>');
		$this->_out('endobj');
		$this->_newobj();
		$this->_out('<<');
		$this->_out('/S /JavaScript');
		$this->_out('/JS '.$this->_textstring($this->javascript));
		$this->_out('>>');
		$this->_out('endobj');
	}


function AutoPrint($dialog=false)
{
	//Open the print dialog or start printing immediately on the standard printer
	$param=($dialog ? 'true' : 'false');
	$script="print($param);";
	$this->IncludeJS($script);
}

function i25($xpos, $ypos, $code, $basewidth=1, $height=10){

    $wide = $basewidth;
    $narrow = $basewidth / 3 ;

    // wide/narrow codes for the digits
    $barChar['0'] = 'nnwwn';
    $barChar['1'] = 'wnnnw';
    $barChar['2'] = 'nwnnw';
    $barChar['3'] = 'wwnnn';
    $barChar['4'] = 'nnwnw';
    $barChar['5'] = 'wnwnn';
    $barChar['6'] = 'nwwnn';
    $barChar['7'] = 'nnnww';
    $barChar['8'] = 'wnnwn';
    $barChar['9'] = 'nwnwn';
    $barChar['A'] = 'nn';
    $barChar['Z'] = 'wn';

    // add leading zero if code-length is odd
    if(strlen($code) % 2 != 0){
        $code = '0' . $code;
    }

    $this->SetFont('Arial','',10);
    $this->Text($xpos, $ypos + $height + 4, $code);
    $this->SetFillColor(0);

    // add start and stop codes
    $code = 'AA'.strtolower($code).'ZA';

    for($i=0; $i<strlen($code); $i=$i+2){
        // choose next pair of digits
        $charBar = $code[$i];
        $charSpace = $code[$i+1];
        // check whether it is a valid digit
        if(!isset($barChar[$charBar])){
            $this->Error('Invalid character in barcode: '.$charBar);
        }
        if(!isset($barChar[$charSpace])){
            $this->Error('Invalid character in barcode: '.$charSpace);
        }
        // create a wide/narrow-sequence (first digit=bars, second digit=spaces)
        $seq = '';
        for($s=0; $s<strlen($barChar[$charBar]); $s++){
            $seq .= $barChar[$charBar][$s] . $barChar[$charSpace][$s];
        }
        for($bar=0; $bar<strlen($seq); $bar++){
            // set lineWidth depending on value
            if($seq[$bar] == 'n'){
                $lineWidth = $narrow;
            }else{
                $lineWidth = $wide;
            }
            // draw every second value, because the second digit of the pair is represented by the spaces
            if($bar % 2 == 0){
                $this->Rect($xpos, $ypos, $lineWidth, $height, 'F');
            }
            $xpos += $lineWidth;
        }
    }
}

function Code39($xpos, $ypos, $code, $baseline=0.5, $height=5){

    $wide = $baseline;
    $narrow = $baseline / 3 ; 
    $gap = $narrow;

    $barChar['0'] = 'nnnwwnwnn';
    $barChar['1'] = 'wnnwnnnnw';
    $barChar['2'] = 'nnwwnnnnw';
    $barChar['3'] = 'wnwwnnnnn';
    $barChar['4'] = 'nnnwwnnnw';
    $barChar['5'] = 'wnnwwnnnn';
    $barChar['6'] = 'nnwwwnnnn';
    $barChar['7'] = 'nnnwnnwnw';
    $barChar['8'] = 'wnnwnnwnn';
    $barChar['9'] = 'nnwwnnwnn';
    $barChar['A'] = 'wnnnnwnnw';
    $barChar['B'] = 'nnwnnwnnw';
    $barChar['C'] = 'wnwnnwnnn';
    $barChar['D'] = 'nnnnwwnnw';
    $barChar['E'] = 'wnnnwwnnn';
    $barChar['F'] = 'nnwnwwnnn';
    $barChar['G'] = 'nnnnnwwnw';
    $barChar['H'] = 'wnnnnwwnn';
    $barChar['I'] = 'nnwnnwwnn';
    $barChar['J'] = 'nnnnwwwnn';
    $barChar['K'] = 'wnnnnnnww';
    $barChar['L'] = 'nnwnnnnww';
    $barChar['M'] = 'wnwnnnnwn';
    $barChar['N'] = 'nnnnwnnww';
    $barChar['O'] = 'wnnnwnnwn'; 
    $barChar['P'] = 'nnwnwnnwn';
    $barChar['Q'] = 'nnnnnnwww';
    $barChar['R'] = 'wnnnnnwwn';
    $barChar['S'] = 'nnwnnnwwn';
    $barChar['T'] = 'nnnnwnwwn';
    $barChar['U'] = 'wwnnnnnnw';
    $barChar['V'] = 'nwwnnnnnw';
    $barChar['W'] = 'wwwnnnnnn';
    $barChar['X'] = 'nwnnwnnnw';
    $barChar['Y'] = 'wwnnwnnnn';
    $barChar['Z'] = 'nwwnwnnnn';
    $barChar['-'] = 'nwnnnnwnw';
    $barChar['.'] = 'wwnnnnwnn';
    $barChar[' '] = 'nwwnnnwnn';
    $barChar['*'] = 'nwnnwnwnn';
    $barChar['$'] = 'nwnwnwnnn';
    $barChar['/'] = 'nwnwnnnwn';
    $barChar['+'] = 'nwnnnwnwn';
    $barChar['%'] = 'nnnwnwnwn';

    $this->SetFont('Arial','',10);
    $this->Text($xpos, $ypos + $height + 4, $code);
    $this->SetFillColor(0);

    $code = '*'.strtoupper($code).'*';
    for($i=0; $i<strlen($code); $i++){
        $char = $code[$i];
        if(!isset($barChar[$char])){
            $this->Error('Invalid character in barcode: '.$char);
        }
        $seq = $barChar[$char];
        for($bar=0; $bar<9; $bar++){
            if($seq[$bar] == 'n'){
                $lineWidth = $narrow;
            }else{
                $lineWidth = $wide;
            }
            if($bar % 2 == 0){
                $this->Rect($xpos, $ypos, $lineWidth, $height, 'F');
            }
            $xpos += $lineWidth;
        }
        $xpos += $gap;
    }
}

function AutoPrintToPrinter($server, $printer, $dialog=false)
{
	//Print on a shared printer (requires at least Acrobat 6)
	$script = "var pp = getPrintParams();";
	if($dialog)
		$script .= "pp.interactive = pp.constants.interactionLevel.full;";
	else
		$script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
	$script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
	$script .= "print(pp);";
	$this->IncludeJS($script);
}


	function _putresources() {
		parent::_putresources();
		if (!empty($this->javascript)) {
			$this->_putjavascript();
		}
	}

	function _putcatalog() {
		parent::_putcatalog();
		if (!empty($this->javascript)) {
			$this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
		}
	}
	

function Rotate($angle,$x=-1,$y=-1)
{
	if($x==-1)
		$x=$this->x;
	if($y==-1)
		$y=$this->y;
	if($this->angle!=0)
		$this->_out('Q');
	$this->angle=$angle;
	if($angle!=0)
	{
		$angle*=M_PI/180;
		$c=cos($angle);
		$s=sin($angle);
		$cx=$x*$this->k;
		$cy=($this->h-$y)*$this->k;
		$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
	}
}

function _endpage()
{
	if($this->angle!=0)
	{
		$this->angle=0;
		$this->_out('Q');
	}
	parent::_endpage();
}

function RotatedText($x,$y,$txt,$angle)
{
    //Text rotated around its origin
    $this->Rotate($angle,$x,$y);
    $this->Text($x,$y,$txt);
    $this->Rotate(0);
}

function RoundedRect($x, $y, $w, $h, $r, $style = '')
    {
        $k = $this->k;
        $hp = $this->h;
        if($style=='F')
            $op='f';
        elseif($style=='FD' || $style=='DF')
            $op='B';
        else
            $op='S';
        $MyArc = 4/3 * (sqrt(2) - 1);
        $this->_out(sprintf('%.2F %.2F m',($x+$r)*$k,($hp-$y)*$k ));
        $xc = $x+$w-$r ;
        $yc = $y+$r;
        $this->_out(sprintf('%.2F %.2F l', $xc*$k,($hp-$y)*$k ));

        $this->_Arc($xc + $r*$MyArc, $yc - $r, $xc + $r, $yc - $r*$MyArc, $xc + $r, $yc);
        $xc = $x+$w-$r ;
        $yc = $y+$h-$r;
        $this->_out(sprintf('%.2F %.2F l',($x+$w)*$k,($hp-$yc)*$k));
        $this->_Arc($xc + $r, $yc + $r*$MyArc, $xc + $r*$MyArc, $yc + $r, $xc, $yc + $r);
        $xc = $x+$r ;
        $yc = $y+$h-$r;
        $this->_out(sprintf('%.2F %.2F l',$xc*$k,($hp-($y+$h))*$k));
        $this->_Arc($xc - $r*$MyArc, $yc + $r, $xc - $r, $yc + $r*$MyArc, $xc - $r, $yc);
        $xc = $x+$r ;
        $yc = $y+$r;
        $this->_out(sprintf('%.2F %.2F l',($x)*$k,($hp-$yc)*$k ));
        $this->_Arc($xc - $r, $yc - $r*$MyArc, $xc - $r*$MyArc, $yc - $r, $xc, $yc - $r);
        $this->_out($op);
    }

    function _Arc($x1, $y1, $x2, $y2, $x3, $y3)
    {
        $h = $this->h;
        $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $x1*$this->k, ($h-$y1)*$this->k,
            $x2*$this->k, ($h-$y2)*$this->k, $x3*$this->k, ($h-$y3)*$this->k));
    }
	
	function Header(){
		//datos SQL
		global $login;
		global $centro_costo;
		global $nom_oficina;
		global $cod_actproy;
		global $des_actproy;
		global $cod_nemonico;
		global $des_nemonico;
		global $title;
		global $nro_nec;
		global $ano_eje;
		global $fch_nec;
		global $tipo_bien;
		global $reqs_anuales;			
		global $topini;
		global $marini;
		global $wcel;
		global $empresaM;
		global $distrito;
		global $departamento;
		global $provincia;
		global $logoCabIzq;
		global $logoSupP1;
		global $logoSupP2;
		global $logoSupP3;
		global $logoSupP4;
		global $marcaAguaP1;
		global $marcaAguaL1;
		global $marcaAguaL2;
		global $marcaAguaL3;
		global $marcaAguaL4;global $marcaAguaP2;
		global $marcaAguaP3;
		global $marcaAguaP4;
		global $marcaDeAgua;
		global $repositorioFirmas;
		global $RUC;
		global $numero_recibo;
		global $codigoContrib;
		global $nombreContrib;
		global $nroDoc;
		global $direccionContrib;
		global $cajero;
		global $fechaEmision;
                global $tipoDoc;
                global $tituloPago;
                global $isArbitrio;
                global $direccionPred;
                global $codPredio;
                global $FraseDelDia;
                global $nroOperacion;
                global $filenameQR;
                global $nEstado;
                global $nDuplicado;
                global $usuario;




                //$nEstado = '0';

                if($nEstado=='0'){
                   /* $this->SetFont('Arial','B',50);
                    $this->SetTextColor(0,192,203);
                    $this->RotatedText(15,150,'ANULADO',45);
                    $this->RotatedText(15,220,'ANULADO',45);*/
                    
                        $this->Image('../img/marca/anulado.png',$marcaAguaL1-55,$marcaAguaL2+51,$marcaAguaL3*0.8,$marcaAguaL4*0.1,'PNG');
                    
                }
                
                //$nDuplicado ='0'; 
                if($nDuplicado=='true' and $nEstado<>'0' ){
                   /* $this->SetFont('Arial','B',50);
                    $this->SetTextColor(255,0,0);
                    $this->RotatedText(10,150,'DUPLICADO',45);
                    $this->RotatedText(10,220,'DUPLICADO',45);*/
                    
                        $this->Image('../img/marca/duplicado.png',$marcaAguaL1-55,$marcaAguaL2+51,$marcaAguaL3*0.8,$marcaAguaL4*0.1,'PNG');
                    
                }

                //$this->Image('../img/marca/escudomarcaagua2.png',$marcaAguaL1-50,$marcaAguaL2,$marcaAguaL3*0.7,$marcaAguaL4*0.7,'PNG');
		$this->SetTextColor(0,0,0);
		
                    $this->Image('../img/marca/escudomarcaagua2.png',$marcaAguaL1-35,$marcaAguaL2+80,$marcaAguaL3*0.4,$marcaAguaL4*0.4,'PNG');
                
		//Logo
		//definimos  ancho y color de rectangulo
		$this->SetLineWidth(0.3);
		$this->SetFillColor(255);
		$this->RoundedRect(1, 1, 75, 60, 1, 'DF');
		$this->RoundedRect(6, 5, 65, 50, 1, 'DF');
		$this->SetFont('arial','B',8);
		$this->SetXY(14,1);
		$this->Cell(5,5,'Municipalidad Distrital de Puente Piedra',0,0,'L');
		$this->SetXY(14,55);
		$this->Cell(5,5,'Municipalidad Distrital de Puente Piedra',0,0,'L');
		//$this->SetXY(5,10);
		$this->RotatedText(5,58,'Municipalidad Distrital de Puente Piedra',90);
		//$this->SetXY(10,10);
		$this->RotatedText(72,5,'Municipalidad Distrital de Puente Piedra',270);
		$this->Image('../img/marca/puenteavanza.png',10,6,58,48,'PNG');
		$this->SetX(1);
		$this->SetFont('narrow','',12);
		$this->Cell(5,17,'Municipalidad Distrital de Puente Piedra',0,0,'L',0);
		$this->ln(3);
		$this->SetX(1);
		$this->Cell(1,22,'Sub Gerencia de Tesoreria',0,0,'L',0);
		$this->ln(3);
		$this->SetX(1);
		$this->Cell(5,27,'RUC '.$RUC,0,0,'L',0);
		$this->ln(3);
		$this->Image('../img/marca/escudo.png',59,66,18,18,'PNG');
		
		$ls = 5; 
		$lw = $marini;
		$lh = $topini;
		
                //$this->AddFont(f25bankprinter, '', 'font/f25bankprinter.php');
		$this->SetXY($lw,$lh += $ls);
		$this->SetFont('f25bankprinter','',14);
		$this->Cell(5,25,'RECIBO',0,0,'L',0);
		$this->SetFont('f25bankprinter','',14);
		$this->SetX(26);
		$this->Cell(40,25,$numero_recibo,0,0,'L',0);
		$this->SetXY($lw,$lh += $ls);
		$this->Cell(5,25,'Codigo ',0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);
		$this->SetX(26);
		$this->SetFont('f25bankprinter','',14);
		$this->Cell(40,25,$codigoContrib,0,0,'L',0);
		$this->SetXY($lw,$lh += $ls);
		$this->SetFont('arial','',10);
		$this->Cell(5,25,'Nombre ',0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);
                if(strlen(trim($nombreContrib))>50){
                    $this->SetFont('narrow','',8);
                }else{
                    $this->SetFont('narrow','',10);
                }
		$this->SetXY($lw+16,$lh+11);
		$this->MultiCell(60,4,trim($nombreContrib),0,'L');
		$this->SetXY($lw,$lh += 2*$ls);
		$this->SetFont('narrow','',12);
		$this->Cell(5,25,$tipoDoc,0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);
		$this->SetXY($lw+16,$lh+11);
		$this->MultiCell(60,4,trim($nroDoc),0,'J');
		//$this->SetXY(5,25);
		//$this->MultiCell(170,3,trim($nroDoc),0,'L');
		/*$this->SetXY($lw,$lh += $ls);
		$this->SetFont('arial','',10);
		$this->Cell(5,25,'Dir. Fis. ',0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);
		$this->SetXY($lw+16,$lh+11);
                if(strlen(trim($direccionContrib))>50){
                    $this->SetFont('narrow','',8);
                }else{
                    $this->SetFont('narrow','',10);
                }
		$this->MultiCell(60,4,trim($direccionContrib),0,'L');
		
                if($isArbitrio=='A'){
                    $this->SetXY($lw,$lh += 2*$ls+2);
                    $this->SetFont('narrow','',12);
                    $this->Cell(5,25,'Predio',0,0,'L',0);
                    $this->SetX(15);
                    $this->Cell(9,25,':',0,0,'L',0);
                    $this->SetXY($lw+16,$lh+11);
                    $this->MultiCell(60,4,trim($codPredio),0,'J');
                    //$this->SetXY(5,25);
                    //$this->MultiCell(170,3,trim($nroDoc),0,'L');
                    $this->SetXY($lw,$lh += $ls);
                    $this->SetFont('narrow','',10);
                    $this->Cell(5,25,'Dir. Pred. ',0,0,'L',0);
                    $this->SetX(15);
                    $this->Cell(9,25,':',0,0,'L',0);
                    $this->SetXY($lw+16,$lh+11);
                    $this->MultiCell(60,4,trim($direccionPred),0,'L');
                }
                */
                $this->SetXY($lw,$lh += 3*$ls);
		
                //$this->Image('../img/marca/barraBoucher_2dat.gif',$lw+50,$lh+6,25,25,'GIF','');
                
                
 
                $this->Image($filenameQR,$lw+50,$lh+6,25,25,'PNG','');
		
                $this->SetXY($lw,$lh - 6);
                $this->SetFont('narrow','',10);
		$this->Cell(5,25,'Generado ',0,0,'L',0);
		$this->SetX(14);
		$this->Cell(9,25,':',0,0,'L',0);
		$this->SetXY($lw+16,$lh+= $ls);
                $this->SetFont('narrow','',12);
		$this->MultiCell(60,4,trim($fechaEmision),0,'J');
		
		
		/*$this->SetXY($lw,$lh += $ls);
		$this->SetFont('narrow','',12);
		$this->Cell(5,25,'Usuario ',0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);*/
		$this->SetXY($lw+16,$lh+= $ls);
		$this->MultiCell(60,4,trim($cajero),0,'J');
                
                $fechaImpresion = strftime('%d-%m-%Y %H:%M:%S');
                $this->SetXY($lw,$lh - 6);
                $this->SetFont('narrow','',10);
		$this->Cell(5,25,'Impreso ',0,0,'L',0);
		$this->SetX(14);
		$this->Cell(9,25,':',0,0,'L',0);
                $this->SetFont('narrow','',12);
		$this->SetXY($lw+16,$lh+= $ls);
		$this->MultiCell(60,4,$fechaImpresion,0,'J');
		
		
		/*$this->SetXY($lw,$lh += $ls);
		$this->SetFont('narrow','',12);
		$this->Cell(5,25,'Usuario ',0,0,'L',0);
		$this->SetX(15);
		$this->Cell(9,25,':',0,0,'L',0);*/
		$this->SetXY($lw+16,$lh+= $ls);
		$this->MultiCell(60,4,trim($usuario),0,'J');
                
		$this->SetXY($lw,$lh += $ls);
		$this->SetFont('f25bankprinter','',12);
		$this->MultiCell(50,4,$tituloPago,0,'C');
		$this->SetXY($lw,$lh += $ls);
		$this->SetFont('f25bankprinter','',12);
		$this->MultiCell(50,4,'',0,'C');
		$this->SetFont('arial','B',10);
		$this->ln(3);
		$this->SetX(1);
		$this->Cell(75,4,'',1,0,'C',0);
		$this->SetX(1);
		$this->Cell(30,4,'Descripcion',0,0,'L',0);
		$this->Cell(45,4,'Monto   ',0,0,'R',0);
		
		
		
		
		
		
	}
	/*
	function Footer(){
		global $marini;
		global $nro_nec;
		global $ano_eje;
		global $nombreGispAdmincon;
                global $tampagina;
		//Posición: a 1,5 cm del final
		$this->SetXY($marini,-25);
		//Arial italic 8
		$this->SetFont('Arial','',10);
		//Número de página
		$this->Cell(79,5,'Municipalidad Distrital de Puente Piedra',0,0,'L',0);
		$this->SetFont('Arial','',8);
		$this->SetXY($marini,-20);
		$this->Cell(0,5,'Pagina N '.$this->PageNo().' de {nb}',0,0,'R');
		$this->SetXY($marini,-20);
		$this->Cell(79,5,'GIT - Gestor Integrado Tributario',0,0,'L',0);
		
	}
	
	function WriteResumen($x,$h, $corte){
		//datos SQL
		global $Coneccion;
		global $ano_eje;
		global $nro_nec;

		//datos DOC
		global $marini;
		global $topmar;
		global $repositorioFirmas;
		

		$this->SetXY($x,$h);
		//Arial italic 8
		$this->SetFont('Arial','',10);
		//Número de página
		$this->MultiCell(79,5,'Comuniquese con nosotros al: ',0,'L');
		$this->SetXY($x,$h+=5);
		$this->MultiCell(79,5,'servicioalcliente@munipuentepiedra.gob.pe',0,'L');
		$this->SetXY($x,$h+=5);
		$this->MultiCell(79,5,'219 6200 - 219 6201',0,'L');
		$this->SetXY($x,$h+=5);
		$this->MultiCell(79,5,'Visitanos en: www.munipuentepiedra.gob.pe',0,'L');
	}
	*/
	
}


$usuario = $_GET['usuario'];
$fechaProyectado =  $_GET['fechaProyectado'];
$contrib =  $_GET['contrib'];
$predio =  $_GET['predio'];
$estado =  $_GET['estado'];
$desde =  $_GET['desde'];
$hasta =  $_GET['hasta'];
$nroOperacion =  trim($_GET['nroOperacion']);
$nroRecibo =  trim($_GET['nroRecibo']);
$copiaCajero = trim($_GET['copiaCajero']);
$nDuplicado = trim($_GET['nDuplicado']);
//$nroOperacion = '1000019857';

$filtroConsulta ="";
if($nroRecibo<>'-1'){
    $filtroConsulta = " AND te.cnumcom = '".$nroRecibo."'";
}
$Rs_grabar = new TSPResult($ConeccionRatania,"");
$Rs_grabar->Poner_MSQL("select 
cnumcom 
from tesoreria.mtesore as te
WHERE te.cnumope like '".$nroOperacion."'
$filtroConsulta 
GROUP BY cnumcom;
");
$Rs_grabar->pg_Poner_Esquema("tesoreria");

$Rs_grabar->executeMSQL();


//echo "<br><br>CONSULTA REQ UPDATE <br> ".$Rs_grabar->Escribir_Consulta();			

$totalRecibos=$Rs_grabar->pg_Num_Rows();


$Rs_TipoPago = new TSPResult($ConeccionRatania,"");
$Rs_TipoPago->Poner_MSQL("SELECT t2.ctippag,
(SELECT t1.vdescri
FROM public.mconten as t1
WHERE t1.idsigma=t2.ctippag) as tipopago,
t2.nmontot
FROM tesoreria.dtesore as t2
WHERE  t2.cnumope='".$nroOperacion."'
ORDER BY ctippag ASC
");
$Rs_TipoPago->pg_Poner_Esquema("tesoreria");

$Rs_TipoPago->executeMSQL();


//echo "<br><br>CONSULTA REQ UPDATE <br> ".$Rs_grabar->Escribir_Consulta();			

$totalTipoPago=$Rs_TipoPago->pg_Num_Rows();


$Rs_grabar = new TSPResult($ConeccionRatania,"");
$Rs_grabar->Poner_MSQL("select sum(te.nmontot) as total, 
                                te.cnumcom, 
                                te.cidpers, 
                                te.vobserv as nombrepersona, 
                                te.ctiprec, 
                                (Select vdescri FROM public.mconten WHERE idsigma=te.ctiprec ) as vdescri, 
                                te.ncantid, 
                                te.imp_insol, 
                                te.nmontot, 
                                te.cnumope, 
                                te.vusernm,
                                te.vhostnm,
                                te.cidapertura,
                                te.nestado, 
                                ( SELECT vdescri FROM tesoreria.destcta Where te.cidecta = cestcta) as glosa,
                                cast(te.dfecpag as varchar(16)) as fecha, 
                                te.vusernm, 
                                te.cnumope, 
                                case when length(coalesce(mp.vnrodoc,'' ))=8 then 'DNI' 
                                    else case 
                                    when length(coalesce(mp.vnrodoc,'' ))=11 then 'RUC' 
                                    else '' end end tipodoc , 
                                coalesce(mp.vnrodoc,'' ) as vnrodoc,
                                to_char(te.dfecpag,'dd-mm-YYYY HH:MI:ss') as fechapag, 
                                cidapertura 
                        from tesoreria.mtesore te  
                            left join public.mperson mp 
                                on mp.idsigma = te.cidpers 
                        where te.cnumope='".$nroOperacion."' 
                            $filtroConsulta 
                        GROUP BY 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22");
$Rs_grabar->pg_Poner_Esquema("tesoreria");

$Rs_grabar->executeMSQL();

//,t1.cnumope,t1.vusernm,t1.hostnm,cidapertura ,t1.vusernm,t1.hostnm,cidapertura,
//echo "<br><br>CONSULTA REQ UPDATE <br> ".$Rs_grabar->Escribir_Consulta();			
$row= $Rs_grabar->pg_Get_Row();


$RUC='20131366702';
$numero_recibo=$row['cnumcom'];
$codigoContrib=$row['cidpers'];
$nombreContrib=  utf8_decode( $row['nombrepersona']);
$direccionContrib=utf8_decode($row['direccionfiscal']);
$cajero=$row['vusernm'];
$fechaEmision=$row['fechapag'];
$nroDoc = $row['vnrodoc'];
$tipoDoc = $row['tipodoc'];


 

$pdf = new PDF('P','mm','A4');




$numRows = $Rs_grabar->pg_Num_Rows();
$N = 0;
$nRecibo = "###";
$sumTotalRecibo = 0;
$sumTotalOperacion = 0;
$sumSubTotalRecibo = 0;
$sumTotalDesc = 0;
$sumTotalAjus = 0;
$sumSubTotalMora = 0;
$sumSubTotalGed = 0;
$nTotalRecibos = 0;
$tamCelda1=50;
$tamCelda2=25;
//echo $nTotalRecibos."<br>";
while($N < $numRows){
    $row= $Rs_grabar->pg_Get_Row();
    if($nRecibo <> $row['cnumcom'] ){

        
    

    $PNG_TEMP_DIR = '/tmp/';

        $nEstado = $row['nestado'];
        $FraseDelDia = " Recibo: ".$row['cnumcom'];
        $FraseDelDia = $FraseDelDia." **Fecha Gen: ".$row['fechapag'];
        $FraseDelDia = $FraseDelDia." **Fecha Gen: ".strftime('%d/%m/%Y');

        $FraseEncrypt = $row['cnumope'].'**'.$row['vusernm'].'**'.$row['vhostnm'].'**'.$row['cidapertura'];
        //echo $FraseDelDia."HOLA";
        $FraseEncrypt = mcrypt_encrypt(MCRYPT_BLOWFISH,$row['cnumope'],$FraseEncrypt,MCRYPT_MODE_CBC);
        $FraseDelDia = $FraseDelDia."**".$FraseEncrypt;
        //echo $FraseDelDia;
        $filenameQR = $PNG_TEMP_DIR.'Recibo'.md5($row['cnumope'].'-'.$row['cnumcom']).'.png';
        //echo    $filenameQR ;    
        QRcode::png($FraseDelDia,$filenameQR,'M','5',2);
        
        $topini = 68;
        $marini = 1; 
        $isArbitrio = $row['controlpago'];
        $codPredio = $row['ccodpre']; 
        $direccionPred = $row['direccion']; 
        $tituloPago = $row['titpag']; 
        //echo $nTotalRecibos."<br>";
        //echo $totalRecibos."<br>";
        
        if($nTotalRecibos<($totalRecibos-1)){
            //echo "PASE POR AQUI ".($nTotalRecibos-1)."<br>";
                $topmar = 145;
                $tampagina = 250+10;
        }elseif($nTotalRecibos==($totalRecibos-1)){
                $topmar = 145;
                $tampagina = 290+($totalTipoPago*8);
            
        }
           

        $lw=1;

        //$pdf->AutoPrint();
        $pdf->AliasNbPages();
        $pdf->SetDisplayMode('fullpage');

        $pdf->AddPage('P',array(80,$tampagina));
        $pdf->SetFont('Arial','B',16);

        $nRecibo = $row['cnumcom'];
        $sumTotalRecibo = 0;
        $sumSubTotalRecibo = 0;
        $sumTotalDesc = 0;
        $sumTotalAjus = 0;
        $sumSubTotalMora = 0;
        $sumSubTotalGed = 0;
        
        $var1 = $topmar+5;
        //$var1 = $topmar+5;
        $lh = $var1;
          

    }    


if($row['tributo']!='AJUS' and $row['tributo']!='DESC'){
    $pdf->SetXY($lw,$lh ); 
    $vdescripcion =  utf8_decode( trim($row['vdescri']).' '.trim($row['glosa']) );
    if(strlen($vdescripcion)<150){
        $tamLetra = 9;
    }  else {
        $tamLetra = 7;
    }
    
    $pdf->SetFont('Arial','B',$tamLetra);
    
    $pdf->MultiCell($tamCelda1,4,$vdescripcion,0,'L');
    $pdf->SetFont('f25bankprinter','',9);
    $pdf->SetXY($lw+$tamCelda1,$lh );
    $pdf->MultiCell($tamCelda2,4,  number_format($row['ncantid'],'0',',',' ').' * '.number_format($row['imp_insol'],'2',',',' '),0,'R');
    $sumSubTotalRecibo += $row['imp_insol']*$row['ncantid'];
    $sumSubTotalMora += $row['imp_mora']+($row['imp_reaj']-$row['imp_insol']);
    $sumSubTotalGed += $row['ged'];
    
    $lh += 5;
    $lz =0;


}
/*
if($row['tributo']=='DESC'){
    $sumTotalDesc += $row['nmonto'];
}
if($row['tributo']=='AJUS'){
    $sumTotalAjus += $row['imp_mora'];
}

*/




$Rs_grabar->pg_Move_Next();
                        
$row2= $Rs_grabar->pg_Get_Row();
if($row2['cnumcom']<>$nRecibo  or $N == ( $numRows -1 ) ){
       //$lh += 5;
    $nTotalRecibos++;
/*    
$pdf->Line($lw, $lh, 75, $lh , $style);
$pdf->SetFont('Arial','B',9);
$pdf->SetXY($lw,$lh );

$pdf->MultiCell($tamCelda1,4,'SUB TOTAL ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumSubTotalRecibo,'2',',',' '),0,'R');

$pdf->SetFont('Arial','B',9);
$pdf->SetXY($lw,$lh +=10);
$pdf->MultiCell($tamCelda1,4,'Mora/Interes ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumSubTotalMora,'2',',',' '),0,'R');
$pdf->SetFont('Arial','B',9);
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell($tamCelda1,4,'GED ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumSubTotalGed,'2',',',' '),0,'R');
$pdf->SetFont('Arial','B',8);
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell($tamCelda1,4,'DESC. ORD. / BENEF. ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumTotalDesc,'2',',',' '),0,'R');
$pdf->SetFont('Arial','B',9);
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell($tamCelda1,4,'Ajuste Redondeo ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumTotalAjus,'2',',',' '),0,'R');
$pdf->SetFont('Arial','B',9);
*/
$sumTotalRecibo = $sumSubTotalRecibo /*+ 
        $sumSubTotalMora + 
        $sumSubTotalGed + 
        $sumTotalDesc + 
        $sumTotalAjus*/;
$pdf->SetXY($lw,$lh +=25);
$pdf->Line($lw, $lh, 75, $lh , $style);
$pdf->MultiCell($tamCelda1,4,'TOTAL ',0,'L');
$pdf->SetFont('f25bankprinter','',10);
$pdf->SetXY($lw+$tamCelda1,$lh );
$pdf->MultiCell($tamCelda2,4,number_format($sumTotalRecibo,'2',',',' '),0,'R');

    if($N<($numRows-1)){
        //$pdf->Image('../img/marca/barraBoucher_1.png',$lw+10,$lh+5,50,20,'PNG','');
        
        if($copiaCajero=='true'){
            $lh = $lh+8;
            $pdf->SetFont('f25bankprinter','',10);
            $pdf->SetXY($lw,$lh );
            $pdf->MultiCell(80,4,'*** COPIA PARA CAJERO ***',0,'C');
            //$lh = $lh+5;
         }else{
            $lh = $lh+4;
        }
         $lh = $lh+8;
        $pdf->Code39($lw+5,$lh+4,$nroOperacion."-".$numero_recibo,0.5,10);

        //$pdf->WriteResumen($lw,$lh+=25, $corte);
        $pdf->SetXY($lw,$lh +=20);
        $pdf->Cell(80,5,'Pagina '.$pdf->PageNo().' de {nb}',0,0,'R');
    }

}




$N++;
}

$pdf->SetFont('Arial','B',9);
$pdf->SetXY($lw,$lh +=10);
$pdf->MultiCell(70,4,'Forma de Pago ',0,'L');
$pdf->SetXY($lw,$lh +=5);
$pdf->Line($lw, $lh, 75, $lh , $style);
$lh+=2;
$NTipoPago=0;
$numRowsTipoPago = $Rs_TipoPago->pg_Num_Rows();
while($NTipoPago < $numRowsTipoPago){
    $row= $Rs_TipoPago->pg_Get_Row();
    
    if(strlen(trim($row['tipopago']))<=20 ){
        $tamLetra=9;
    }elseif(strlen(trim($row['tipopago']))>20 and strlen(trim($row['tipopago']))<=30){
        $tamLetra=8;
    }elseif(strlen(trim($row['tipopago']))>30 and strlen(trim($row['tipopago']))<=40){
        $tamLetra=7;
    }
    $pdf->SetXY($lw,$lh );
    $pdf->SetFont('Arial','B',$tamLetra);
    
    $pdf->MultiCell($tamCelda1,4,trim($row['tipopago']),0,'L');
    $pdf->SetFont('f25bankprinter','',10);
    $pdf->SetXY($lw+$tamCelda1,$lh );
    $pdf->MultiCell($tamCelda2,4,number_format($row['nmontot'],'2',',',' '),0,'R');
    /*$pdf->SetXY($lw,$lh +=5);
    $pdf->MultiCell(30,4,'Vuelto ',0,'L');
    $pdf->SetFont('f25bankprinter','',10);
    $pdf->SetXY($lw+30,$lh );
    $pdf->MultiCell(45,4,'77.20',0,'R');*/
    $lh+=8;
    $NTipoPago++;
    $Rs_TipoPago->pg_Move_Next();        
    
}

//$pdf->Image('../img/marca/barraBoucher_1.png',$lw+10,$lh+5,50,20,'PNG','');
//Codigo de Barras I25
//$pdf->i25($lw,$lh ,$nroOperacion."0".$numero_recibo);

if($copiaCajero=='true'){
    $pdf->SetFont('f25bankprinter','',10);
    $pdf->SetXY($lw,$lh );
    $pdf->MultiCell(80,4,'*** COPIA PARA CAJERO ***',0,'C');
    $lh = $lh+5;
}else{
    $lh = $lh+4;
}
    
$pdf->Code39($lw+5,$lh,$nroOperacion."-".$numero_recibo,0.5,10);

//$pdf->WriteResumen($lw,$lh+=25, $corte);
$pdf->SetXY($lw,$lh +=25);
$pdf->Cell(80,5,'Pagina '.$pdf->PageNo().' de {nb}',0,0,'R');
$pdf->SetXY($lw,$lh +=5);
//Arial italic 8
$pdf->SetFont('Arial','',10);
//Número de página
$pdf->MultiCell(79,5,'Comuniquese con nosotros al: ',0,'L');
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell(79,5,'servicioalcliente@munipuentepiedra.gob.pe',0,'L');
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell(79,5,'219 6200 - 219 6201',0,'L');
$pdf->SetXY($lw,$lh +=5);
$pdf->MultiCell(79,5,'Visitanos en: www.munipuentepiedra.gob.pe',0,'L');

$pdf->Output();


?>